/**
 * Created by WT-SUN on 2017/6/6.
 */

$(function () {
    var date = new Date();
    var year = date.getFullYear();
    $('#planTb').treegrid({
        striped: true,
        idField:'id',
        treeField:'name',
        fit: true,
        singleSelect: true,
        fitColumns: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/train/plan/selectAnalysisPerson.shtml',
        queryParams:{
            yearAnnual:year
        },
        columns: [[
            {field: 'name', title: '单位', width: 100},
            {field: 'num1', title: '计划人次', width: 100,formatter: function (value, row, index) {
                var aValue=typeof (row.num1)=='undefined'?"0":row.num1;
                return aValue;
            }},
            {field: 'num2', title: '完成人次', width: 100},
            {field: 'aa3', title: '未完成人次', width: 100,formatter: function (value, row, index) {
                var aValue=typeof (row.num1)=='undefined'?"0":(row.num1*1-row.num2*1);
                return aValue;
            }},
            {field: 'aa1', title: '人次完成率', width: 100,formatter: function (value, row, index) {
                var aValue=typeof (row.num1)=='undefined'?"0":((row.num2*1)/(row.num1*1)*100).toFixed(2);
                return aValue+"%";
            }}
        ]]
    });
    /**
     * 高级查询
     */
    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#planTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
        var date = new Date();
        var year = date.getFullYear();
        $("#yearAnnual").combobox('select', year);
        $("#planStartMonth").val("");
    });

    queryYearthList("yearAnnual");
    var TRAIN_PROTYPE=CONSTANT.DictionaryTypeDefConstants.TRAIN_PROTYPE;//专业类别标识
    queryComboData(TRAIN_PROTYPE,'professionalTypeId');
    var TRAIN_LEVEL=CONSTANT.DictionaryTypeDefConstants.TRAIN_LEVEL;//培训级别标识
    queryComboData(TRAIN_LEVEL,'trainLevelId');
    queryDepartmentTree();
    planStartMonth();
});
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'};
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            valueField:'id',
            textField:'text',
            onLoadSuccess: function () { //加载完成后,设置选中第一项
                $(this).combobox('select', yearthData[0].id);
            }
        }
    );
}
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            deptCom('searchAddDept',result);
        }
    });
}
function deptCom(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            require:true,
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            }
        }
    );
}
function queryComboData(code,queryId) {
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    var pageId='#'+queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                onSelect: function (record) {
                }
            });
        }
    });
}
function planStartMonth() {
    var monthData = new Array();
    monthData.push({id:"",text:"请选择"});
    for(var i =1;i<13;i++){
        if(i<10){
            var y = {id:'0'+i,text:(i)+'月'};
        }else {
            var y = {id:i,text:(i)+'月'};
        }

        monthData.push(y);
    }
    $('#monthId').combobox(
        {
            data:monthData,
            valueField:'id',
            textField:'text',
            onSelect: function (r) {
                if (r.id!=''){
                    var yValue=$("#yearAnnual").val();
                    $("#planStartMonth").val(yValue+"-"+r.id);
                }else {
                    $("#planStartMonth").val("");
                }
            }
        }
    );
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#planTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    $('#planTb').treegrid('options').queryParams = queryParams;
    $("#planTb").treegrid('reload');
}

